package thread.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-25 18:02
 **/
public class TicketRunnable implements Runnable {
    private static int i = 100;
    ReentrantLock lo = new ReentrantLock();

    @Override
    public void run() {

        while (true) {
            lo.lock();
            //通过finally下面的unlock

            try {

                if (i > 0) {

                    System.out.println(Thread.currentThread().getName() + "消费了第: " + i + " 张票");
                    i--;
                } else {
                    break;

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lo.unlock();

            }
        }
    }
}
